package com.xs.ahoi.hcxs.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * author: ahoi
 * date: 2018/10/8 10:16
 * description:
 */
public class NetWorkUtil {

    // 没有网络
    private static final int NONETWORK = 0;
    // 当前是wifi连接
    private static final int WIFI = 1;
    // 不是wifi连接
    private static final int NOWIFI = 2;

    /**
     * 检测当前网络的类型 是否是wifi
     */
    public static int checkedNetWorkType(Context context) {
        if (!checkedNetWork(context)) {
            return NONETWORK;
        }
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting()) {
            return WIFI;
        } else {
            return NOWIFI;
        }
    }

    /**
     * 检查是否连接网络
     */
    public static boolean checkedNetWork(Context context) {
        // 获得连接设备管理器
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm == null) return false;
        // 获取网络连接对象
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        return networkInfo != null && networkInfo.isAvailable();
    }
}
